<?php
class MimomentsAction extends Action{
	public function _initialize(){

		$this->retVal = Session::init();
		if (!$this->retVal) {
			Output::instance()->resultCode(code_access_denied);
			exit;
		}
		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$this->mimoment_db = D('Mimoments');
		$this->comment_db = D('MimomentsComment');
		$this->praise_db = D('MimomentsPraise');
		$this->mimoment_tag_relation_db = D('MimomentsTagRelation');
		$this->miactivity_partake_db = D('miactivity_partake');

		$this->cid = $cid;
		Session::switchCustomer($cid);
	}
	public function update_comment(){
		   // $_POST = $_GET;
		$status = $_POST['status'];

		if(empty($_POST['id'])){
			$data = array();
			$data['cid'] = $this->cid;
			$data['activityId'] = $_POST['activityId'];
			unset($_POST['activityId']);
			$data['createTime'] = date('Y-m-d H:i:s');
			$partakeId = $this->miactivity_partake_db->add($data);
			$_POST['partakeId'] = $partakeId;
		}

		$result_update = $this->comment_db->update();
		$result = array();
		if ($result_update) {
			$result['code'] = 1;
			$result['id'] = $result_update;
			$this->sendTmpInfo($result_update);
			if($status == 1){
				$this->add_read_record($result_update);
			}
			

		} else {
			$result['code'] = 0;
			$error = $this->mimoment_db->getError();
			$result['info'] = $error?$error:$this->comment_db->_sql();
		}
		Output::instance()->object($result);
	}
	
	public function add_read_record($commentId){
		$map = array();
		$map['c.id'] = $commentId;
		$map['c.status'] = 1;
		$map['c.deleted'] = 0;
		$momentsInfo = M('mimoments_comment')->alias('c')
		->field('c.*')
		->where($map)->find();
		if($momentsInfo){
			$partakeId = $momentsInfo['partakeId'];
			$momentsId = $momentsInfo['momentsId'];
			$customerId = $this->cid;
			$userId =  0;
			if($momentsId > 0){
				Session::switchCustomer(0);
				$moments_map = array();
				$moments_map['deleted'] = 0;
				$moments_map['status'] = 1;
				$moments_map['id'] = $momentsId;
				$moments_user_id = M('mimoments')->where($moments_map)->getField('userId');
				$comment_map = $praise_map = array();
				$comment_map['deleted'] = 0;
				$comment_map['status'] = 1;
				$comment_map['momentsId'] = $momentsId;
				$comment_user_list = M('mimoments_comment')->where($comment_map)->distinct(true)->getField('id,userId');
				$praise_map['deleted'] = 0;
				$praise_map['momentsId'] = $momentsId;
				$prasie_user_list = M('mimoments_praise')->where($praise_map)->distinct(true)->getField('id,userId');

				$user_list = array();
				$user_list[] = $moments_user_id;

				if($comment_user_list){
					$user_list = array_merge($user_list,$comment_user_list);
				}


				if($prasie_user_list){
					$user_list = array_merge($user_list,$prasie_user_list);
				}
				$user_list = array_unique($user_list);

				$read_data_list = $read_data = array();
				foreach ($user_list as $key => $value) {
					if($value != $userId){
						$read_data['deleted'] = 0 ;
						$read_data['partakeId'] = $partakeId;
						$read_data['cid'] = $customerId;
						$read_data['userId'] = $value;
						$read_data['createTime'] = date('Y-m-d H:i:s');
						$read_data_list[] = $read_data;
					}
				}
				if($read_data_list&&count($read_data_list) > 0){
					M('mimoments_read')->addAll($read_data_list);
				}
			}
		}

	}
	private function sendTmpInfo($commentId){
		
		
		$customerId = $this->cid;
		$data = array();
		$tplType = '';
		// $actId = $param->actId;
		// $momentsId = $param->momentsId;

				//需要审核并且发布留言的人不是管理员的情况下才会发送审核通知

		Session::switchCustomer(0);
		$map = array();
		$map['c.id'] = $commentId;
		$map['c.status'] = 1;
		$map['c.deleted'] = 0;
		$momentsInfo = M('mimoments')->alias('a')
		->field('a.activityId,a.content,b.openid,c.comment,c.momentsId')
		->join('inner join wx_'.$customerId.'_user_weixin_info b on a.userId=b.userId')
		->join('inner join wx_mimoments_comment c on c.momentsId=a.id')
		->where($map)->find();
		log::write('sql:'.M('mimoments')->_sql());
		if($momentsInfo){
			$momentsId = $momentsInfo['momentsId'];
			$tplType = 'ConsultationReplyMessage_OPENTM202109783';
			$first = '您好，您有一条新的消息回复';
			$momentsOpenid = $momentsInfo['openid'];
			$keyword1 = strip_tags($momentsInfo['content']);
			$keyword2 = strip_tags($momentsInfo['comment']);

			$remark = '请点击查看详情';
			$data['first'] = array('value'=> $first,'color'=>"#000000");
			$data['keyword1'] = array('value'=> $keyword1,'color'=>'#000000');
			$data['keyword2'] = array('value'=> $keyword2,'color'=>'#000000');

			$data['remark'] = array('value'=> $remark,"color"=>'#000000');
			$customerId = $this->cid;
			$actId = $momentsInfo['activityId'];
			$url = C('SERVER_HOST').'call.html?customerId='.$customerId.'&module=moments&method=join_now&params={"actId":'.$actId.',"momentsId":'.$momentsId.'}#/detail/'.$momentsId;
				// $url = '';
				// $url = urlencode($url);
			$log_data = $data;
			$log_data['url'] = $url;
			$log_data['momentsOpenid'] = $momentsOpenid;
			$log_data['tplType'] = $tplType;
			$log_data['customerId'] = $customerId;
			Log::write('comment_notice:'.json_encode($log_data));



			TemplateMessage::send($momentsOpenid,$customerId,$tplType,$url,$data);
		}


		

	}

	public function update(){
		
		$result_update = $this->mimoment_db->update();
		$result = array();
		if ($result_update) {
			$result['code'] = 1;
			$result['id'] = $result_update;
		} else {
			$result['code'] = 0;
			$error = $this->mimoment_db->getError();
			$result['info'] = $error?$error:"发生未知错误";
		}
		Output::instance()->object($result);
	}
	public function setTags($moments_id,$tags){
		$result = $mimoment_tag_relation_list = $mimoment_tag_relation_data = array();
		$tag_id_array = split(',', $tags);
		$create_time = date('Y-m-d H:i:s');
		foreach ($tag_id_array as $key => $value) {
			$mimoment_tag_relation_data['momentsId'] = $moments_id;
			$mimoment_tag_relation_data['tagId'] = $value;
			if($this->mimoment_tag_relation_db->where($mimoment_tag_relation_data)->count() <= 0){
				$mimoment_tag_relation_data['createTime'] = $create_time;
				$mimoment_tag_relation_list[] =  $mimoment_tag_relation_data;
			}
			
		}
		$add_result = $this->mimoment_tag_relation_db->addAll($mimoment_tag_relation_list);
		if($add_result){
			$result['code'] = 1;
			$result['info'] = '标签设置成功';
		}else{
			$result['code'] = 0;
			$error = $error = $this->mimoment_tag_relation_db->getError();
			$result['info'] = $error?$error:$this->mimoment_tag_relation_db->_sql();
		}

		Output::instance()->object($result);
	}
	/*
	*获取留言列表
	*/
	public function getMomentsList($activity_id,$channel_id=0,$order = 'id',$by='desc',$start, $limit){
		Session::switchCustomer(0);
		$map = array();
		$map['c.activityId'] = $activity_id;
		$map["c.deleted"] = 0;
   	// $map["a.cid"] = $cid;
   	// $map['c.name|e.nickname|c.phone'] = array(array('like','%'.$keyword.'%'),array('like','%'.$keyword.'%'),array('like','%'.$keyword.'%'),'_multi'=>true);
		if($channel_id > 0){
			$map['c.channelId'] = $channel_id;
		}


		$list = $this->mimoment_db->alias('a')
		->field('a.*,c.lat,c.lng,c.address_detail,c.channelId,c.activityId,e.nickname,e.headimgurl')
		->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')      				
		->join('left join '.C('DB_PREFIX').$this->cid.'_user_weixin_info e  on a.userId=e.userId')
		->where($map) 
		->distinct(true)       			
		->order($order.' '.$by)->limit($start.', '.$limit)->select();


        // echo $this->mimoment_db->_sql();
        // exit;
		if ($list) {
			$comment_map = $tag_mag =  array(); 
			foreach ($list as $key => $value) {
				$moments_id = $value['id'];
				$tag_mag['a.momentsId'] = $moments_id;
				$tag_list = $this->mimoment_tag_relation_db->alias('a')
				->field('a.*,c.tagName')
				->join('inner join '.C('DB_PREFIX').'mimoments_tag c on a.tagId=c.id')      				
				->where($tag_mag)
				->select();
				$value['tag_list'] = $tag_list;
				$comment_map['a.momentsId'] = $moments_id;
				$comment_map['a.deleted'] = 0;
				$comment_list = $this->comment_db->alias('a')
				->field('a.*,e.nickname,e.headimgurl')
				->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')      				
				->join('left join '.C('DB_PREFIX').$this->cid.'_user_weixin_info e  on a.userId=e.userId')
				->where($comment_map)
				->select();
				foreach ($comment_list as $i => $comment) {
					$comment_list[$i]["comment"] = html_entity_decode($comment["comment"]);
					$replyId = $comment["replyId"];
					foreach($comment_list as $j =>$reply){
						if($replyId == $reply["id"]){
							$comment_list[$i]["replyName"] = $reply["nickname"];
						}
					}
				}
				$value['comment_list'] = $comment_list;
				$praise_list = $this->praise_db->alias('a')
				->field('a.*,e.nickname,e.headimgurl')
				->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')      				
				->join('inner join '.C('DB_PREFIX').$this->cid.'_user_weixin_info e  on a.userId=e.userId')
				->where($comment_map)
				->select();
   		 	// echo $this->comment_db->_sql();
   		 	// echo $this->praise_db->_sql();
   		 	// exit;
				$value['praise_list'] = $praise_list;
				$list[$key] = $value;
			}
			$count = $this->mimoment_db->alias('a')
			->field('a.*,c.channelId,c.activityId')
			->join('inner join '.C('DB_PREFIX').'miactivity_partake c on a.partakeId=c.id')      				
			->join('inner join '.C('DB_PREFIX').$this->cid.'_user_weixin_info e  on a.userId=e.userId')
			->where($map) 

			->distinct(true)         
			->count();
			Session::switchCustomer($cid);
			$result['code'] = 1;
			$result['list'] = $list;
			$result['count'] = $count;

		} else {
			$result['code'] = 0;
			$result['info'] = '没有更多数据';
		}
		Output::instance()->object($result);
	}
	public function getFacebox(){
		$facebox = array();
		$face = new stdClass();
		$face->id=0;
		$face->mark = '[微笑]';
		$face->url = 'mp/assets/imgs/facebox/0.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=1;
		$face->mark = '[撇嘴]';
		$face->url = 'mp/assets/imgs/facebox/1.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=2;
		$face->mark = '[色]';
		$face->url = 'mp/assets/imgs/facebox/2.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=3;
		$face->mark = '[发呆]';
		$face->url = 'mp/assets/imgs/facebox/3.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=4;
		$face->mark = '[得意]';
		$face->url = 'mp/assets/imgs/facebox/4.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=5;
		$face->mark = '[流泪]';
		$face->url = 'mp/assets/imgs/facebox/5.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=6;
		$face->mark = '[害羞]';
		$face->url = 'mp/assets/imgs/facebox/6.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=7;
		$face->mark = '[闭嘴]';
		$face->url = 'mp/assets/imgs/facebox/7.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=8;
		$face->mark = '[睡]';
		$face->url = 'mp/assets/imgs/facebox/8.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=9;
		$face->mark = '[大哭]';
		$face->url = 'mp/assets/imgs/facebox/9.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=10;
		$face->mark = '[尴尬]';
		$face->url = 'mp/assets/imgs/facebox/10.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=11;
		$face->mark = '[发怒]';
		$face->url = 'mp/assets/imgs/facebox/11.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=12;
		$face->mark = '[调皮]';
		$face->url = 'mp/assets/imgs/facebox/12.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=13;
		$face->mark = '[呲牙]';
		$face->url = 'mp/assets/imgs/facebox/13.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=14;
		$face->mark = '[惊讶]';
		$face->url = 'mp/assets/imgs/facebox/14.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=15;
		$face->mark = '[难过]';
		$face->url = 'mp/assets/imgs/facebox/15.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=16;
		$face->mark = '[酷]';
		$face->url = 'mp/assets/imgs/facebox/16.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=17;
		$face->mark = '[冷汗]';
		$face->url = 'mp/assets/imgs/facebox/17.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=18;
		$face->mark = '[抓狂]';
		$face->url = 'mp/assets/imgs/facebox/18.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=19;
		$face->mark = '[吐]';
		$face->url = 'mp/assets/imgs/facebox/19.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=20;
		$face->mark = '[偷笑]';
		$face->url = 'mp/assets/imgs/facebox/20.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=21;
		$face->mark = '[可爱]';
		$face->url = 'mp/assets/imgs/facebox/21.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=22;
		$face->mark = '[白眼]';
		$face->url = 'mp/assets/imgs/facebox/22.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=23;
		$face->mark = '[傲慢]';
		$face->url = 'mp/assets/imgs/facebox/23.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=24;
		$face->mark = '[饥饿]';
		$face->url = 'mp/assets/imgs/facebox/24.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=25;
		$face->mark = '[困]';
		$face->url = 'mp/assets/imgs/facebox/25.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=26;
		$face->mark = '[惊恐]';
		$face->url = 'mp/assets/imgs/facebox/26.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=27;
		$face->mark = '[流汗]';
		$face->url = 'mp/assets/imgs/facebox/27.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=28;
		$face->mark = '[憨笑]';
		$face->url = 'mp/assets/imgs/facebox/28.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=29;
		$face->mark = '[大兵]';
		$face->url = 'mp/assets/imgs/facebox/29.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=30;
		$face->mark = '[奋斗]';
		$face->url = 'mp/assets/imgs/facebox/30.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=31;
		$face->mark = '[咒骂]';
		$face->url = 'mp/assets/imgs/facebox/31.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=32;
		$face->mark = '[疑问]';
		$face->url = 'mp/assets/imgs/facebox/32.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=33;
		$face->mark = '[嘘]';
		$face->url = 'mp/assets/imgs/facebox/33.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=34;
		$face->mark = '[晕]';
		$face->url = 'mp/assets/imgs/facebox/34.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=35;
		$face->mark = '[折磨]';
		$face->url = 'mp/assets/imgs/facebox/35.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=36;
		$face->mark = '[衰]';
		$face->url = 'mp/assets/imgs/facebox/36.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=37;
		$face->mark = '[骷髅]';
		$face->url = 'mp/assets/imgs/facebox/37.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=38;
		$face->mark = '[敲打]';
		$face->url = 'mp/assets/imgs/facebox/38.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=39;
		$face->mark = '[再见]';
		$face->url = 'mp/assets/imgs/facebox/39.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=40;
		$face->mark = '[擦汗]';
		$face->url = 'mp/assets/imgs/facebox/40.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=41;
		$face->mark = '[抠鼻]';
		$face->url = 'mp/assets/imgs/facebox/41.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=42;
		$face->mark = '[鼓掌]';
		$face->url = 'mp/assets/imgs/facebox/42.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=43;
		$face->mark = '[糗大了]';
		$face->url = 'mp/assets/imgs/facebox/43.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=44;
		$face->mark = '[坏笑]';
		$face->url = 'mp/assets/imgs/facebox/44.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=45;
		$face->mark = '[左哼哼]';
		$face->url = 'mp/assets/imgs/facebox/45.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=46;
		$face->mark = '[右哼哼]';
		$face->url = 'mp/assets/imgs/facebox/46.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=47;
		$face->mark = '[哈欠]';
		$face->url = 'mp/assets/imgs/facebox/47.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=48;
		$face->mark = '[鄙视]';
		$face->url = 'mp/assets/imgs/facebox/48.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=49;
		$face->mark = '[委屈]';
		$face->url = 'mp/assets/imgs/facebox/49.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=50;
		$face->mark = '[快哭了]';
		$face->url = 'mp/assets/imgs/facebox/50.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=51;
		$face->mark = '[阴险]';
		$face->url = 'mp/assets/imgs/facebox/51.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=52;
		$face->mark = '[亲亲]';
		$face->url = 'mp/assets/imgs/facebox/52.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=53;
		$face->mark = '[吓]';
		$face->url = 'mp/assets/imgs/facebox/53.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=54;
		$face->mark = '[可怜]';
		$face->url = 'mp/assets/imgs/facebox/54.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=55;
		$face->mark = '[菜刀]';
		$face->url = 'mp/assets/imgs/facebox/55.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=56;
		$face->mark = '[西瓜]';
		$face->url = 'mp/assets/imgs/facebox/56.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=57;
		$face->mark = '[啤酒]';
		$face->url = 'mp/assets/imgs/facebox/57.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=58;
		$face->mark = '[篮球]';
		$face->url = 'mp/assets/imgs/facebox/58.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=59;
		$face->mark = '[乒乓]';
		$face->url = 'mp/assets/imgs/facebox/59.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=60;
		$face->mark = '[咖啡]';
		$face->url = 'mp/assets/imgs/facebox/60.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=61;
		$face->mark = '[饭]';
		$face->url = 'mp/assets/imgs/facebox/61.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=62;
		$face->mark = '[猪头]';
		$face->url = 'mp/assets/imgs/facebox/62.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=63;
		$face->mark = '[玫瑰]';
		$face->url = 'mp/assets/imgs/facebox/63.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=64;
		$face->mark = '[凋谢]';
		$face->url = 'mp/assets/imgs/facebox/64.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=65;
		$face->mark = '[示爱]';
		$face->url = 'mp/assets/imgs/facebox/65.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=66;
		$face->mark = '[爱心]';
		$face->url = 'mp/assets/imgs/facebox/66.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=67;
		$face->mark = '[心碎]';
		$face->url = 'mp/assets/imgs/facebox/67.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=68;
		$face->mark = '[蛋糕]';
		$face->url = 'mp/assets/imgs/facebox/68.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=69;
		$face->mark = '[闪电]';
		$face->url = 'mp/assets/imgs/facebox/69.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=70;
		$face->mark = '[炸弹]';
		$face->url = 'mp/assets/imgs/facebox/70.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=71;
		$face->mark = '[刀]';
		$face->url = 'mp/assets/imgs/facebox/71.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=72;
		$face->mark = '[足球]';
		$face->url = 'mp/assets/imgs/facebox/72.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=73;
		$face->mark = '[瓢虫]';
		$face->url = 'mp/assets/imgs/facebox/73.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=74;
		$face->mark = '[便便]';
		$face->url = 'mp/assets/imgs/facebox/74.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=75;
		$face->mark = '[月亮]';
		$face->url = 'mp/assets/imgs/facebox/75.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=76;
		$face->mark = '[太阳]';
		$face->url = 'mp/assets/imgs/facebox/76.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=77;
		$face->mark = '[礼物]';
		$face->url = 'mp/assets/imgs/facebox/77.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=78;
		$face->mark = '[拥抱]';
		$face->url = 'mp/assets/imgs/facebox/78.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=79;
		$face->mark = '[强]';
		$face->url = 'mp/assets/imgs/facebox/79.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=80;
		$face->mark = '[弱]';
		$face->url = 'mp/assets/imgs/facebox/80.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=81;
		$face->mark = '[握手]';
		$face->url = 'mp/assets/imgs/facebox/81.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=82;
		$face->mark = '[胜利]';
		$face->url = 'mp/assets/imgs/facebox/82.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=83;
		$face->mark = '[抱拳]';
		$face->url = 'mp/assets/imgs/facebox/83.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=84;
		$face->mark = '[勾引]';
		$face->url = 'mp/assets/imgs/facebox/84.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=85;
		$face->mark = '[拳头]';
		$face->url = 'mp/assets/imgs/facebox/85.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=86;
		$face->mark = '[差劲]';
		$face->url = 'mp/assets/imgs/facebox/86.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=87;
		$face->mark = '[爱你]';
		$face->url = 'mp/assets/imgs/facebox/87.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=88;
		$face->mark = '[NO]';
		$face->url = 'mp/assets/imgs/facebox/88.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=89;
		$face->mark = '[OK]';
		$face->url = 'mp/assets/imgs/facebox/89.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=90;
		$face->mark = '[爱情]';
		$face->url = 'mp/assets/imgs/facebox/90.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=91;
		$face->mark = '[飞吻]';
		$face->url = 'mp/assets/imgs/facebox/91.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=92;
		$face->mark = '[跳跳]';
		$face->url = 'mp/assets/imgs/facebox/92.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=93;
		$face->mark = '[发抖]';
		$face->url = 'mp/assets/imgs/facebox/93.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=94;
		$face->mark = '[呕火]';
		$face->url = 'mp/assets/imgs/facebox/94.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=95;
		$face->mark = '[转圈]';
		$face->url = 'mp/assets/imgs/facebox/95.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=96;
		$face->mark = '[磕头]';
		$face->url = 'mp/assets/imgs/facebox/96.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=97;
		$face->mark = '[回头]';
		$face->url = 'mp/assets/imgs/facebox/97.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=98;
		$face->mark = '[跳绳]';
		$face->url = 'mp/assets/imgs/facebox/98.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=99;
		$face->mark = '[挥手]';
		$face->url = 'mp/assets/imgs/facebox/99.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=100;
		$face->mark = '[激动]';
		$face->url = 'mp/assets/imgs/facebox/100.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=101;
		$face->mark = '[街舞]';
		$face->url = 'mp/assets/imgs/facebox/101.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=102;
		$face->mark = '[献吻]';
		$face->url = 'mp/assets/imgs/facebox/102.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=103;
		$face->mark = '[右太极]';
		$face->url = 'mp/assets/imgs/facebox/103.gif';
		array_push($facebox,$face);
		$face = new stdClass();
		$face->id=104;
		$face->mark = '[左太极]';
		$face->url = 'mp/assets/imgs/facebox/104.gif';
		array_push($facebox,$face);
		$result = array();
		$result['code'] = 1;
		$result['list'] = $facebox;
		Output::instance()->object($result);
        // return $facebox;
	}
	public function getSummary($activity_id,$returnJson=true){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		} 
		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$miactivity_partake_db = M('miactivity_partake');
		$miactivity_visit_db = M('miactivity_visit');
		$map = array();
		$map['a.activityId'] = $activity_id;
		$map['a.cid'] = $cid;
		$map['a.deleted'] = 0;
		$map['a.userId'] = array('gt',0);
		// $map['c.deleted'] = 0;
		// $map['h.deleted'] = 0;
		// $map['j.deleted'] = 0;
		$list = $miactivity_partake_db->alias('a')
		->field('a.*,e.activityname,c.id as mimoments_id,c.picUrls,h.id as mimoments_comment_id,j.id as mimoments_praise_id,d.id as channel_id,d.name as channel_name,f.createTime as visit_time,f.id as visit_id,f.userId as visit_user_id')
		->join('left join '.C('DB_PREFIX').'mimoments c on a.id=c.partakeId')
		->join('left join '.C('DB_PREFIX').'mimoments_comment h on a.id=h.partakeId') 
		->join('left join '.C('DB_PREFIX').'mimoments_praise j on a.id=j.partakeId')     				
		->join('left join '.C('DB_PREFIX').'miactivity_channel d on a.channelId=d.id')    
		->join('left join '.C('DB_PREFIX').'miactivity_visit f on f.partakeId=a.id')
		//->join('left join '.C('DB_PREFIX').'miactivity_visit ')
		->join('inner join '.C('DB_PREFIX').'miactivity e on a.activityId=e.id')
		
		->where($map)->order('a.createTime asc')->select();
		// echo $miactivity_partake_db->_sql();
		// exit;
		$visit_count  = 0;
		$partake_count = 0;
		$partake_id = 0;
		$photo_count = 0;

		$moments_count = 0;
		$comment_count = 0;
		$praise_count = 0;
		$channel_data = array();
		$date_data = array();

		$moments_data = array();
		$comment_data = array();
		$praise_data = array();

		foreach ($list as $key => $value) {
			$activityname = $value['activityname'];
			$date_day = date('Y-m-d',strtotime($value['createTime']));
			$date_data[$date_day]['date_day'] = $date_day;
			
			if($value['mimoments_id'] && !$moments_data[$value['mimoments_id']]){
				$moments_count ++;
				$moments_data[$value['mimoments_id']] = $value['mimoments_id'];

			}

			if($value['mimoments_comment_id']&&!$comment_data[$value['mimoments_comment_id']]){
				$comment_count ++;
				$comment_data[$value['mimoments_comment_id']] = $value['mimoments_comment_id'];
			}

			if($value['mimoments_praise_id']&&!$praise_data[$value['mimoments_praise_id']]){
				$praise_count ++;
				$praise_data[$value['mimoments_praise_id']] = $value['mimoments_praise_id'];
			}

			if($value['channel_id']){
				$channel_data[$value['channel_id']]['channel_name'] = $value['channel_name'];
				$channel_data[$value['channel_id']]['channel_id'] = $value['channel_id'];

			}

			if($value['visit_id']){
				// $visit_count++;
				// $visit_user_id = $value['visit_user_id'];
				// if($value['channel_id']){
				// 	if(!in_array($visit_user_id,$channel_data[$value['channel_id']]['user_id'])){
				// 		$channel_data[$value['channel_id']]['visit_count'] ++;
				// 		$channel_data[$value['channel_id']]['user_id'][] = $visit_user_id;
				// 	}

				// }
				// if(!in_array($visit_user_id, $date_data[$date_day]['user_id'])){
				// 	$date_data[$date_day]['visit_count']++;
				// 	$date_data[$date_day]['user_id'][] =  $visit_user_id;
				// }
				
			}

			if($partake_id != $value['id']){
				$photo_share_count = count(explode(',',$value['picUrls']));
				if($value['channel_id']){
					$channel_data[$value['channel_id']]['partake_count'] ++;
					$channel_data[$value['channel_id']]['photo_count'] =$channel_data[$value['channel_id']]['photo_count'] + $photo_share_count ;
				}
				$date_data[$date_day]['partake_count']++;
				$date_data[$date_day]['photo_count'] = $date_data[$date_day]['photo_count'] + $photo_share_count ;
				$partake_count++;
				$photo_count += $photo_share_count;
				$partake_id = $value['id'];
			}
		}
		log::write('moments_data:'.json_encode($moments_data));
		log::write('comment_data:'.json_encode($comment_data));
		log::write('praise_data:'.json_encode($praise_data));
		$moments_count = count($moments_data);
		$comment_count = count($comment_data);
		$praise_count = count($praise_data);
		$map = array();
		$map['a.activityId'] = $activity_id;
		$map['a.cid'] = $cid;
		
		$visit_list = $miactivity_visit_db->alias('a')
		->field('a.*,d.name')
		->join('left join '.C('DB_PREFIX').'miactivity_channel d on a.channelId=d.id')    
		->where($map)->select();
		log::write('vist_list'.json_encode($vist_list));
		log::write('visit_sql',$miactivity_visit_db->_sql());
		foreach ($visit_list as $key => $value) {

			$date_day = date('Y-m-d',strtotime($value['createTime']));
			$date_data[$date_day]['date_day'] = $date_day;
			// $date_data[$date_day]['visit_count']++;
			if(!$value['channelId']){
				$channelId = '无';
				$channelName = '无';
			}else{
				$channelId = $value['channelId'];
				$channelName = $value['name'];
			}
			// $channel_data[$channelId]['visit_count'] ++;
			$channel_data[$channelId]['channel_name'] = $channelName;
            $visit_user_id = $value['userId'];
			if($channelId){
				if(!in_array($visit_user_id,$channel_data[$channelId]['user_id'])){
					$channel_data[$channelId]['visit_count'] ++;
					$channel_data[$channelId]['user_id'][] = $visit_user_id;
				}

			}
			if(!in_array($visit_user_id, $date_data[$date_day]['user_id'])){
				$date_data[$date_day]['visit_count']++;
				$date_data[$date_day]['user_id'][] =  $visit_user_id;
			}



		}
		
		// foreach ($list as $key => $value) {
		// 	if($value['visit_time']){
		// 		$visit_user_id = $value['visit_user_id'];
		// 		$date_day = date('Y-m-d',strtotime($value['visit_time']));
		// 		if($date_data[$date_day]){

		// 		}else{
		// 			if(!in_array($visit_user_id, $date_data[$date_day]['user_id'])){
		// 				$date_data[$date_day]['visit_count']++;
		// 				$date_data[$date_day]['user_id'][] =  $visit_user_id;
		// 			}
		// 			// $date_data[$date_day]['visit_count'] ++;
		// 			$date_data[$date_day]['date_day']  = $date_day;
		// 		}
		// 	}
		// }
		foreach ($channel_data as $key => $value) {
			if(!$value['visit_count']){
				$value['visit_count'] = 0;
			}
			if(!$value['photo_count']){
				$value['photo_count'] = 0;
			}
			if(!$value['partake_count']){
				$value['partake_count'] = 0;
			}
			$channel_data[$key] = $value;
		}
		foreach ($date_data as $key => $value) {
			if(!$value['visit_count']){
				$value['visit_count'] = 0;
			}
			if(!$value['photo_count']){
				$value['photo_count'] = 0;
			}
			if(!$value['partake_count']){
				$value['partake_count'] = 0;
			}
			$date_data[$key] = $value;
		}
		

		$sql = 'select count(*) as visit_count from (SELECT  userId  FROM `'.C('DB_PREFIX').'miactivity_visit` WHERE ( `cid` = '.$cid.' ) AND ( `activityId` = '.$activity_id.' ) AND ( `deleted` = 0 ) GROUP BY userId ) t ;';
		log::write('count_sql:'.$sql);
		$visit_count_search = M()->query($sql);
		
		$visit_count = $visit_count_search[0]['visit_count'];

		$summary_data = array();
		$summary_data['moments_count'] = $moments_count;
		$summary_data['comment_count'] = $comment_count;
		$summary_data['praise_count'] = $praise_count;

		$summary_data['partake_count'] = $partake_count;
		$summary_data['photo_count'] = $photo_count;
		$summary_data['visit_count'] = $visit_count;
		$summary_data['channel_data'] = $channel_data;
		$summary_data['date_data'] = $date_data;
		$summary_data['activityname'] = $activityname;
		if($list){
			Session::switchCustomer($cid);
			if($returnJson==true){
				Output::instance()->object($summary_data);
			}else{
				return $summary_data;
			}
			
		}else{
			Output::instance()->resultCode();
		}

	}
    //获取用户统计
	public function getUserSummary($activity_id,$returnJson=true){
		
		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$miactivity_partake_db = M('miactivity_partake');
		
		$map = array();
		$map['a.activityId'] = $activity_id;
		$map['a.cid'] = $cid;
		$map['a.deleted'] = 0;
		$map['a.userId'] = array('gt',0);
		$list = $miactivity_partake_db->alias('a')
		->field('a.*,d.nickname,d.headimgurl,e.activityname')
		->join('inner join '.C('DB_PREFIX').$this->cid.'_user_weixin_info d  on a.userId=d.userId')
		->join('inner join '.C('DB_PREFIX').'miactivity e on a.activityId=e.id')
		->where($map)->order('a.createTime asc')->select();

		$user_data = $user_data_tmp =  array();


		foreach ($list as $key => $value) {
			$activityname = $value['activityname'];
			$user_data[$value['userId']]['user_info'] = $value;
			$user_data[$value['userId']]['count'] ++;
		}
		foreach ($user_data as $key => $value) {
			$user_data_tmp[] = $value;
		}
		$user_data = list_sort_by($user_data_tmp,'count','desc');
		$summary_data = array();


		
		$summary_data['user_data'] = $user_data;
		$summary_data['list_data'] = $list;
		$summary_data['activityname'] = $activityname;
		if($list){
			Session::switchCustomer($cid);
			if($returnJson==true){
				Output::instance()->object($summary_data);
			}else{
				return $summary_data;
			}
			
		}else{
			Output::instance()->resultCode();
		}

	}

	/**
	 * 获取留言统计
	 * @param $activity_id
	 * @param bool $returnJson
	 * @return array
	 */

	public function getMomentsSummary($activity_id,$returnJson=true){

		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$miactivity_partake_db = M('miactivity_partake');
		$moments_table = M('mimoments');

		$map = array();
		$map['a.activityId'] = $activity_id;
		$map['a.cid'] = $cid;
		$map['a.deleted'] = 0;
		$map['a.userId'] = array('gt',0);

		//查出标签对应的留言下的评论数(未删除的,会排除掉一部分留言)
		//$map['i.id'] = 31,测试
		$commentList = $miactivity_partake_db->alias('a')
		->field('c.id as mimoments_id,i.id as tagId,i.tagName,e.activityname, t.id as commentId, t.deleted as deleted')
		->join('inner join '.C('DB_PREFIX').'mimoments c on a.id=c.partakeId')
		->join('left join '.C('DB_PREFIX').'mimoments_comment t on c.id=t.momentsId')
		->join('left join '.C('DB_PREFIX').'mimoments_tag_relation h on c.id=h.momentsId')
		->join('left join '.C('DB_PREFIX').'mimoments_tag i on i.id=h.tagId')  
		->join('inner join '.C('DB_PREFIX').'miactivity e on a.activityId=e.id')
		->where($map)->order('a.createTime asc')->select();

		$comment_data = $praise_data =  array();
		if($commentList){
			//对相关评论数据进行处理
			$activityname = $commentList[0]['activityname'];
			foreach($commentList as $k=>$value){
				$tagName = $value['tagName']?$value['tagName']:'无标签';
				$comment_data[$value['tagId']]['tagName'] = $tagName;  //对应标签
				if($value['deleted'] !== null && $value['deleted'] != 1){
					$comment_data[$value['tagId']]['commentCount'] ++;    //对应评论数
				}
				if($comment_data[$value['tagId']]['mimoments_id'] !== $value['mimoments_id']){
					$comment_data[$value['tagId']]['mimomentsCount']++;   //对应留言数
				}
				$comment_data[$value['tagId']]['mimoments_id'] = $value['mimoments_id'];
			}
		}

		//查询标签对应下留言的点赞数
		$praiseList = $miactivity_partake_db->alias('a')
			->field('c.id as mimoments_id,i.id as tagId,i.tagName,e.activityname, p.id as praiseId, p.deleted as deleted')
			->join('inner join '.C('DB_PREFIX').'mimoments c on a.id=c.partakeId')
			->join('left join '.C('DB_PREFIX').'mimoments_praise p on c.id=p.momentsId')
			->join('left join '.C('DB_PREFIX').'mimoments_tag_relation h on c.id=h.momentsId')
			->join('left join '.C('DB_PREFIX').'mimoments_tag i on i.id=h.tagId')
			->join('inner join '.C('DB_PREFIX').'miactivity e on a.activityId=e.id')
			->where($map)->order('a.createTime asc')->select();

		if($praiseList){
			//对相关点赞数据进行处理
			foreach($praiseList as $pk=>$pVal){
				$tagName = $pVal['tagName']?$pVal['tagName']:'无标签';
				$praise_data[$pVal['tagId']]['tagName'] = $tagName;  //对应标签
				if($pVal['deleted'] !== null && $pVal['deleted'] != 1){
					$praise_data[$pVal['tagId']]['praiseCount'] ++;    //对应点赞数
				}
				if($praise_data[$pVal['tagId']]['mimoments_id'] !== $pVal['mimoments_id']){
					$praise_data[$pVal['tagId']]['mimomentsCount']++;  //对应留言数
				}
				$praise_data[$pVal['tagId']]['mimoments_id'] = $pVal['mimoments_id'];

			}
		}

		$resultArr = $resultArrs = array();
		//合并点赞以及评论的数据
		foreach($comment_data as $k=>$v){
			$resultArr[] = array_merge($v,$praise_data[$k]);
		}

		$resultArr = list_sort_by($resultArr,'mimomentsCount','desc');
		foreach($resultArr as $k=>$v){
			if(!$v['praiseCount']){
				$v['praiseCount'] = 0;
			}
			unset($v['mimoments_id']);
			if(!$v['commentCount']){
				$v['commentCount'] = 0;
			}
			$resultArrs['data'][] = $v;
			$resultArrs['tagName'][] = $v['tagName'];
			$resultArrs['commentCount'][] = $v['commentCount'];
			$resultArrs['praiseCount'][] = $v['praiseCount'];
			$resultArrs['mimomentsCount'][] = $v['mimomentsCount'];

		}


		/*echo '<pre>';
		var_dump($resultArrs);
		echo '</pre>';exit;*/

		$summary_data = array();
		$summary_data['data'] = $resultArrs;
		if(!empty($activityname)){
			$summary_data['activityname'] = $activityname;
		}else{
			$summary_data['activityname'] = '未知活动';
		}

		if($commentList){
			Session::switchCustomer($cid);
			if($returnJson==true){
				Output::instance()->object($summary_data);
			}else{
				return $summary_data;
			}
			
		}else{
			Output::instance()->resultCode();
		}

	}


/**
	 * 晒照片活动数据导出
	 */
public function exportPhotoShare($actId,$type="day"){
	$result = array();
		$expTitle = '留言活动参与信息' ; //设置文件名
		$fileName = '留言活动参与信息' . date('Ymd'); //设置文件名

		switch ($type) {
			//按日期统计
			case 'day':
			$summary_data = $this->getSummary($actId,false);
			$activity_name = $summary_data['activityname'];
			$expCellName  = array(
				array('date_day','日期'),
				array('partake_count','参与人数'),
				array('visit_count','浏览人数')
				);
			$expTableData = $summary_data['date_data'];
			$fileName = $activity_name.'_按日期统计_' . date('Ymd');
			break;
			//按渠道统计
			case 'channel':
			$summary_data = $this->getSummary($actId,false);
			$activity_name = $summary_data['activityname'];
			$expCellName  = array(
				array('channel_name','渠道'),
				array('partake_count','参与人数'),
				array('visit_count','浏览人数')
				);
			$expTableData = $summary_data['channel_data'];
			$fileName = $activity_name.'_按渠道统计_' . date('Ymd');
			break;
			//用户统计
			case 'user':
			$summary_data = $this->getUserSummary($actId,false);
			$activity_name = $summary_data['activityname'];
			$expCellName  = array(
				array('nickname','昵称'),
				array('count','互动次数')
				);
			$user_data = array();
			$user_data_tmp = $summary_data['user_data'];
			foreach ($user_data_tmp as $key => $value) {
				$user_data[$key]['nickname'] = $value['user_info']['nickname'];
				$user_data[$key]['count'] = $value['count'];
			}
			$expTableData = $user_data;
			$fileName = $activity_name.'_按用户统计_' . date('Ymd');
			break;
			//留言分类
			case 'type':
			$summary_data = $this->getMomentsSummary($actId,false);
			$expTableData = $summary_data['data']['data'];
			$activity_name = $summary_data['activityname'];
			$expCellName  = array(
				array('tagName','分类名称'),
				array('mimomentsCount','留言数'),
				array('commentCount','评论数'),
				array('praiseCount','点赞数')
				);
			$fileName = $activity_name.'_留言分类统计_' . date('Ymd');
			break;
			default:

			break;
		}
		$expTableDataTemp = array();
		foreach ($expTableData as $key => $value) {
			$expTableDataTemp[] = $value;
		}
	/*echo '<pre>';
	var_dump($fileName);
	echo '</pre>';exit;*/
		if($expTableDataTemp){
			Output::instance()->objArrayExcel($fileName,$expCellName,$expTableDataTemp);
		}
	    // else{
	    // 	$result['code'] = 0;
	    // 	$result['info'] = '暂无数据';
	    // 	Output::instance()->object($result);
	    // }

        // if($expTableData){
        // 	$this->exportExcelNoTitle($fileName,$expTitle,$expCellName,$expTableData);
        // }

		
	}

	

   /*
   *获取活动参加记录
   */
   public function getPartakeList($activity_id,$keyword='',$channel_id=0,$order = 'id',$by='desc',$start = 0, $limit = 10){
   	$retVal = Session::init();
   	if (!$retVal) {
   		Output::instance()->resultCode(code_access_denied);
   		return;
   	} 
   	$cid = Session::$user["customerId"];
   	Session::switchCustomer(0);
   	$miactivity_visit_db = M("miactivity_visit");
   	$partake_db = M('miactivity_partake');

   	$map = array();
   	$map['activityId'] = $activity_id;
   	$map["a.deleted"] = 0;
   	$map["a.cid"] = $cid;
   	$map['c.name|e.nickname|c.phone'] = array(array('like','%'.$keyword.'%'),array('like','%'.$keyword.'%'),array('like','%'.$keyword.'%'),'_multi'=>true);
   	if($channel_id > 0){
   		$map['a.channelId'] = $channel_id;
   	}

   	$list = $partake_db->alias('a')
   	->field('a.*,c.name as user_name,c.phone,c.picUrls,d.name as channel_name,e.nickname,e.headimgurl')
   	->join('inner join '.C('DB_PREFIX').'miactivity_photo_share c on a.id=c.partakeId')      				
   	->join('left join '.C('DB_PREFIX').'miactivity_channel d on a.channelId=d.id')    
   	->join('left join '.C('DB_PREFIX').$cid.'_user_weixin_info e  on a.userId=e.userId')
   	->where($map) 
   	->distinct(true)       			
   	->order($order.' '.$by)->limit($start.', '.$limit)->select();

   	$visit_map = array();
   	foreach ($list as $key => $value) {
   		$value['picUrls'] = explode(',', $value['picUrls']);
   		$visit_map['partakeId'] = $value['id'];
   		$visit_map['activityId'] = $activity_id;
   		$visit_map['cid'] = $cid;
   		$value['visit_count'] = $miactivity_visit_db->where($visit_map)->count();
   		$list[$key] = $value;
   	}

   	if ($list) {
   		$count = $partake_db->alias('a')
   		->field('a.*,c.name as user_name,c.phone,c.picUrls,d.name as channel_name')
   		->join('inner join '.C('DB_PREFIX').'miactivity_photo_share c on a.id=c.partakeId')      		
   		->join('left join '.C('DB_PREFIX').'miactivity_channel d on a.channelId=d.id')    
   		->join('left join '.C('DB_PREFIX').$cid.'_user_weixin_info e  on a.userId=e.userId')
   		->where($map) 
   		->distinct(true)         
   		->count();
   		Session::switchCustomer($cid);
   		Output::instance()->objArray($list,$count);
   	} else {
   		Output::instance()->resultCode();
   	}

   }

   public function del($id){

   	$map = array();
   	$map['id'] = $id;
   	$result_del = $this->miactivity_channel_db->where($map)->setField('deleted',1);
   	$result = array();
   	if ($result_del) {
   		$result['code'] = 1;
   	} else {
   		$result['code'] = 0;
   		$error = $this->miactivity_channel_db->getError();
   		$result['info'] = $error?$error:"发生未知错误";
   	}
   	Output::instance()->object($result);
   }
}